<?php
add_action('wp_head', 'submit_coupon');
function submit_coupon(){ ?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
        $('input[name="store_name"]').autocomplete({
                source: "auto-complete",
                minLength: 2  
            });
        var frm = $('#coupon_submit');
        frm.submit(function(event){
           event.preventDefault();
           var store_url = $('input[name="store_name"]').val();
           var code = $('input[name="coupon_code"]').val();
           var description = $('textarea[name="des_cp"]').val();
           var expri_date = $('input[name="ex_date"]').datepicker({ dateFormat: 'yy/mm/dd' }).val();
           var security = $('input[name="6_letters_code"]').val();
           var title_coupon = $('input[name="title_coupon"]').val();
           var value_sending = [store_url,code,description,expri_date,security,title_coupon];
           var data ={
                    action : 'submit_coupon',
                    value_send : value_sending
                };
           jQuery.post(ajaxurl, data, function(result) {
                    if(result == 'captcha fail'){
                        $('div.captcha-fail').removeClass('hiden');
                    }
                    if(result == 'store fail'){
                        $('div.domain-fail').removeClass('hiden');
                    }
                    if(result == 'submit success'){
                        $('div.from-add-cp').empty();
                        var html='<div class="row-fluid success_submit">';
                        html += '<p>Thank for share coupon!</p>';
                        html += '</div>';
                        $('div.submit_wg').after(html);
                    }
                });
        });
    });
</script>
<?php }
add_action('wp_ajax_nopriv_submit_coupon', 'submit_coupon_callback');
add_action('wp_ajax_submit_coupon', 'submit_coupon_callback');
function submit_coupon_callback(){
    session_start();
    global $wpdb,$table_prefix;
    $value_query = $_POST['value_send'];
    $store_url = $value_query[0];
    $code = $value_query[1];
    $description = $value_query[2];
    $expri_date = $value_query[3];
    $security = $value_query[4];
    $title = $value_query[5];
    if($_SESSION['6_letters_code'] == $security){
        $my_query ="
            SELECT post_id FROM ".$table_prefix."postmeta WHERE meta_key ='url_store_metadata' AND meta_value LIKE '%$store_url'
        ";
        $result = $wpdb->get_results($my_query);
        $store_id = $result[0]->post_id;
            if($store_id != 0){
                $new_store = array(
                            'post_title' => $title,
                            'post_type' =>'coupon',
                            'post_status'=>'draft',
                            'post_content' => $description
                        );
                        $id = wp_insert_post($new_store,true);
                        add_post_meta($id, store_coupon_metadata, $store_id);
                        add_post_meta($id, expire_date_metadata, $expri_date);
                        add_post_meta($id, coupon_code_metadata, $code);
                 echo 'submit success';
            }else{
                echo 'store fail';
            }
    }
    else{
        echo 'captcha fail';
    }
    die();
}
add_action('wp_ajax_nopriv_sugget_url', 'sugget_url_callback');
add_action('wp_ajax_sugget_url', 'sugget_url_callback');
function sugget_url_callback(){
    $value_query = $_POST['value_send'];
    $args = array(
        'post_type' => 'store',
        'post_status' => 'publish',
        'meta_query' => array(
            array(
                'key' => 'url_store_metadata',
                'value' => $value_query,
                'compare' => 'LIKE'
            )
        )
    );
    $query = new WP_Query($args);
    $store = $query->posts;
    //echo $value_query;
    foreach ($store as $st){
        echo "<p><a id='url_return' style='cursor:pointer' >".get_post_meta($st->ID,'url_store_metadata',true)."</a></p>";
    }
    die();
}
?>